{{!

  Copyright 2016 Facebook, Inc.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

}}{{!

This is a top level C++ file that generates an implementation of the C++
Services interface. That implementation keeps a reference to a Pyobject, which
is supposed to be the Python handler for that service, as written by the end
user. It has lightweight wrappers of each service method (which accept futures)
that pass the python object and a promise return code into the functions
defined in ServicesCallbacks, along with appropriate arguments.

}}
{{> common/AutoGeneratedC}}

#include <{{program:includePrefix}}gen-py3/{{program:name}}/services_wrapper.h>
{{#program:hasServiceFunctions?}}
{{! This one is generated by cython rather than thrift:  If there are no
    functions (empty service), then it will not exist
}}
#include <{{program:includePrefix}}gen-py3/{{program:name}}/services_api.h>
{{/program:hasServiceFunctions?}}
#include <thrift/lib/cpp2/async/AsyncProcessor.h>

{{#program:cppNamespaces}}namespace {{value}} {
{{/program:cppNamespaces}}
{{#program:services}}

{{! Manage the PyObject if it's the base class }}
{{^service:extends?}}
{{service:name}}Wrapper::{{service:name}}Wrapper(PyObject *obj, folly::Executor* exc)
  : if_object(obj), executor(exc)
  {
    {{#program:hasServiceFunctions?}}
    import_{{#program:py3Namespaces}}{{value}}__{{/program:py3Namespaces}}{{program:name}}__services();
    {{/program:hasServiceFunctions?}}
    Py_XINCREF(this->if_object);
  }

{{service:name}}Wrapper::~{{service:name}}Wrapper() {
    Py_XDECREF(this->if_object);
}
{{/service:extends?}}
{{! Otherwise, proxy to the parent class to do the memory management}}
{{#service:extends?}}
{{service:name}}Wrapper::{{service:name}}Wrapper(PyObject *obj, folly::Executor* exc)
  : {{#service:extends}}{{#service:cppNamespaces}}{{value}}::{{/service:cppNamespaces}}{{service:name}}Wrapper(obj, exc){{/service:extends}}
  {
    {{#program:hasServiceFunctions?}}
    import_{{#program:py3Namespaces}}{{value}}__{{/program:py3Namespaces}}{{program:name}}__services();
    {{/program:hasServiceFunctions?}}
  }
{{/service:extends?}}

{{#service:functions}}
folly::Future<{{#function:returnType}}{{!
        }}{{^program:stack_arguments?}}{{> types/CppArgType}}{{/program:stack_arguments?}}{{!
        }}{{#program:stack_arguments?}}{{> types/CppValueType}}{{/program:stack_arguments?}}{{!
    }}{{/function:returnType}}>{{!
}} {{service:name}}Wrapper::future_{{function:name}}({{#function:args}}
  {{#field:type}}{{!
          }}{{^program:stack_arguments?}}{{> types/CppArgType}}{{/program:stack_arguments?}}{{!
          }}{{#program:stack_arguments?}}{{> types/CppStackArgType}}{{/program:stack_arguments?}}{{!
      }}{{/field:type}} {{field:name}}{{^last?}},{{/last?}}{{#last?}}
{{/last?}}{{/function:args}}) {
  {{#function:returnType}}folly::Promise<{{!
          }}{{^program:stack_arguments?}}{{> types/CppArgType}}{{/program:stack_arguments?}}{{!
          }}{{#program:stack_arguments?}}{{> types/CppValueType}}{{/program:stack_arguments?}}{{!
      }}{{/function:returnType}}> promise;
  auto future = promise.getFuture();
  auto ctx = getConnectionContext();
  folly::via(
    this->executor,
    [this, ctx,
     promise = std::move(promise){{#function:args}},
     {{#field:type}}{{> CppFieldCapture}}{{/field:type}}{{/function:args}}
    ]() mutable {
        call_cy_{{service:name}}_{{function:name}}(
            this->if_object,
            ctx,
            std::move(promise){{#function:args}}{{#field:type}},
            {{> CppPassField}}
{{/field:type}}{{/function:args}}
        );
    });

  return future;
}

{{/service:functions}}
std::shared_ptr<apache::thrift::ServerInterface> {{service:name}}Interface(PyObject *if_object, folly::Executor *exc) {
  return std::make_shared<{{service:name}}Wrapper>(if_object, exc);
}
{{^last?}}

{{/last?}}
{{/program:services}}{{!
}}{{#program:cppNamespaces}}
} // namespace {{value}}
{{/program:cppNamespaces}}
